<?php
/*
********1、输出打印值echo********
*/
echo "Hello ";
echo "world!";
echo "<br/>";
echo "I am Jason";
echo "<br/>";
/*
********2、变量********
*/
$one = "apple";
$two = "pear";
/*
********3、使用 . 连接字符串********
*/
echo $one." ".$two;
echo "<br/>";
/*
********4、索引数组********
*/
//索引数组（类似于C语言）
$fruit=array(1,2,3);
$fruits = array($one,$two);
//调试打印var_dump()
var_dump($fruit);
//var_dump($fruits)
/*
********5、索引数组遍历foreach********
*/
  //不需要索引值
foreach($fruits as $v){
    echo $v;
    echo "<br/>";
 }
  //需要索引值
foreach($fruits as $k=>$v){
   echo ($k+1).$v;
   echo "<br/>";
}
/*
********6、处理用户请求传值********
*/
//传值：?stuno=17101001&class=17101
//$_REQUEST系统变量
var_dump($_REQUEST);
//$_GET  $_POST
$stuno = $_REQUEST["stuno"];
$class = $_REQUEST["class"];
echo $stuno.$class;
echo "<br/>";

/*
********7、关联数组********
*/
//关联数组定义
$stu1 = array(
    "stuno"=>"1701",
    "name"=>"张三"
);
//关联数组遍历
foreach($stu1 as $key=>$value){
    echo $key.$value;
    echo "<br/>";
}

/*
********8、函数定义和调用********
*/
//函数定义
function print_stu($stu){
    foreach($stu as $key=>$value){
        echo $key.$value;
    }

}

//函数调用
print_stu($stu1);

/*
********9、PHP操作MySQL********
*/
$serve = 'localhost:3306';
$username = 'root';
$password = '';
$dbname = 'student';
//连接数据库
$link = mysqli_connect(
    $serve,$username,$password,
    $dbname);
// 设置数据库字符集
mysqli_set_charset($link,'UTF8'); 
//查询数据库
$result = mysqli_query($link,
      'select * from student');
// 获取数据 
$data = mysqli_fetch_all(
    $result,MYSQLI_ASSOC); 
var_dump($result);
// 释放结果集 
mysqli_free_result($result); 
var_dump($data);
//自行补充使用foreach遍历$data
echo "<h4>学生列表</h4>";
echo "<table border=1>";
echo "<tr><th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>班级</th>
</tr>";
foreach($data as $one){
    echo "<tr>";
    foreach($one as $key=>$value){
        if($key!="id")
        echo "<td>".$value."</td>";
    }
    echo "</tr>";
}
echo "</table>";
?>